#include "../import/head.h"
#include "odbc_init_load.h"
#include "../import/import.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "helper_odbc.h"

////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_sgsj
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    odbc_init_load::odbc_init_load()
    {
    }

    odbc_init_load::~odbc_init_load()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // proc

	// 
	// do load work
	// 
    int odbc_init_load::run_task( xos::i_msg * pMsg )
    {
        int ret = 0;
		
		if( 0 == ret )
		{
			ret = load_building_type();
		}
		
		if( 0 == ret )
		{
			ret = load_city_type();
		}

		if( 0 == ret )
		{
			ret = load_email_type();
		}

		if( 0 == ret )
		{
			ret = load_equipment_place();
		}

		if( 0 == ret )
		{
			ret = load_equipment_type();
		}

		if( 0 == ret )
		{
			ret = load_hero_type();
		}

		if( 0 == ret )
		{
			ret = load_item_category();
		}

		if( 0 == ret )
		{
			ret = load_land_type();
		}

		if( 0 == ret )
		{
			ret = load_process_type();
		}

		if( 0 == ret )
		{
			ret = load_relation_type();
		}

		if( 0 == ret )
		{
			ret = load_report_type();
		}

		if( 0 == ret )
		{
			ret = load_soldier_type();
		}

		if( 0 == ret )
		{
			ret = load_suit();
		}

		if( 0 == ret )
		{
			ret = load_task_type();
		}

		if( 0 == ret )
		{
			ret = load_technology_type();
		}

		if( 0 == ret )
		{
			ret = load_title();
		}

		if( 0 == ret )
		{
			ret = load_un();
		}

		if( 0 == ret )
		{
			ret = load_user();
		}
		
		pMsg->set_bool( 0, 0 == ret );

		{
			data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
			p->init();
			p->m_nMsg = MSG_DB;
			p->m_pMsg = pMsg;
			g_pSysData->m_pImpl->notify( p );
			pMsg = 0;
			p = 0;
		}

        return ret;
    }

	// 
	// disp load result
	// 
    int odbc_init_load::proc( xos::i_msg * pMsg )
    {
        int ret = 0;

		session * pSession = (session *)pMsg->get_void( 0, 0 );
		bool bRet = pMsg->get_bool( 0, 0 );

		if( 0 == ret )
		{
			data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
			p->init();
			p->m_nMsg = MSG_DB_INIT_LOADED;
			p->m_bSuccess = bRet;
			g_pSysData->m_pImpl->notify( p );
			p = 0;
		}

		if( ( 0 == ret ) && pSession )
		{
			if( bRet )
			{
				pSession->sub_ref( 1 );
			}
			else
			{
				pSession->sub_ref( 2 );
			}
		}

		if( pMsg )
		{
			pMsg->release();
			pMsg = 0;
		}

        return ret;
    }

	// 
	// invoke db work
	// 
	int odbc_init_load::run( session * pSession )
	{
		int ret = 0;

		xos::i_msg * pMsg = 0;

		if( 0 == ret )
		{
			ret = g_pSysData->m_os.m_pXos->create( xos::i_xos::XOS_OBJ_MSG, (void **)&pMsg );
		}

		if( 0 == ret )
		{
			pMsg->set_int( 0, OPT_INIT_LOAD_DB );
			pMsg->set_int( 1, helper_odbc::DB_OPT_INIT_LOAD );
			pMsg->set_void( 0, pSession );
		}

		if( ( 0 == ret ) && pSession )
		{
			pSession->add_ref( 1 );
		}

		if( 0 == ret )
		{
			g_pSysData->m_os.odbc_notify( pMsg );
			pMsg = 0;
		}

		if( pMsg )
		{
			pMsg->release();
			pMsg = 0;
		}

		return ret;
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// helper funtion

	int odbc_init_load::load_building_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = building_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_building_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_city_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = city_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_city_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_email_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = email_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_email_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_equipment_place()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = equipment_place::get( 0, 0, -1, &g_pSysData->m_pPool->m_equipment_place_list );
		}

		return ret;
	}

	int odbc_init_load::load_equipment_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = equipment_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_equipment_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_hero_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = hero_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_hero_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_item_category()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = item_category::get( 0, 0, -1, &g_pSysData->m_pPool->m_item_category_list );
		}

		return ret;
	}

	int odbc_init_load::load_land_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = land_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_land_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_process_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = process_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_process_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_relation_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = relation_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_relation_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_report_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = report_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_report_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_soldier_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = soldier_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_soldier_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_suit()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = suit::get( 0, 0, -1, &g_pSysData->m_pPool->m_suit_list );
		}

		return ret;
	}

	int odbc_init_load::load_task_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = task_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_task_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_technology_type()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = technology_type::get( 0, 0, -1, &g_pSysData->m_pPool->m_technology_type_list );
		}

		return ret;
	}

	int odbc_init_load::load_title()
	{
		int ret = 0;

		if( 0 == ret )
		{
			ret = title::get( 0, 0, -1, &g_pSysData->m_pPool->m_title_list );
		}

		return ret;
	}

	int odbc_init_load::load_un()
	{
		int ret = 0;
		return ret;
	}

	int odbc_init_load::load_user()
	{
		int ret = 0;
		return ret;
	}
	
} // xos_sgsj
